package com.ch.d0415;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class 线程池 {

    public static void main(String[] args) {

        // 数量不限的线程池
        ExecutorService es1 = Executors.newCachedThreadPool();
        // 指定数量的线程池
        ExecutorService es2 = Executors.newFixedThreadPool(10);
        //延迟或定时执行任务的线程池
        Executors.newScheduledThreadPool(10);

        for (int i = 0;i < 20;i++) {
           final int ii = i;
            es2.execute( ()->{
                System.out.println("工作: " + ii + "开始!");
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("工作: " + ii + "结束!");
            });
        }
    }
}

class ThreadPool {
    Map map = new Hashtable<>();
    List list = new Vector<>();

    // 将非同步集合 转为 同步集合
    // 连接池
    Set<Thread> pool =Collections.synchronizedSet(new HashSet<>());
    // 运行的连接池
    Set<Thread> runs =Collections.synchronizedSet(new HashSet<>());

    {
        // 随机获取一个set中的元素
        Thread thread = pool.stream().findFirst().get();
    }

    ThreadPool(int size){
        for(int i = 0;i < size;i++) {
            Thread t = new Thread();
            pool.add(t);
        }
    }

    void execute(Runnable command) {

    }


}

